#!/bin/sh

# Copyright (C) 2006-2013 Bart Martens <bartm@knars.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

die_hard() {
	echo "ERROR: $1" >&2
	echo "More information might be available at:" >&2
	echo "  http://wiki.debian.org/PepperFlashPlayer" >&2
	exit 1
}

[ "$(id -u)" = "0" ] || die_hard "must be root"

show_usage() {
	echo "Usage:"
	echo "  update-pepperflashplugin-nonfree --install"
	echo "  update-pepperflashplugin-nonfree --uninstall"
	echo "  update-pepperflashplugin-nonfree --status"
	echo "Additional options:"
	echo "  --verbose"
	echo "  --quiet"
	exit 1
}

getopt_temp=$(getopt -o iusfvq --long install,uninstall,status,fast,verbose,quiet \
	-n 'update-pepperflashplugin-nonfree' -- "$@") || show_usage
eval set -- "$getopt_temp"

ACTION=none
fast=no
verbose=no
quiet=no

while [ $# -gt 0 ]
do
	case "$1" in
		-i|--install)
			ACTION="--install"
			shift
			;;
		-u|--uninstall)
			ACTION="--uninstall"
			shift
			;;
		-s|--status)
			ACTION="--status"
			shift
			;;
		-f|--fast)
			fast=yes
			shift
			;;
		-v|--verbose)
			verbose=yes
			shift
			;;
		-q|--quiet)
			quiet=yes
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!"
			exit 1
			;;
	esac
done

[ "$ACTION" != "none" -a $# -eq 0 ] || show_usage
[ "$quiet" != "yes" ] || verbose=no

[ "$verbose" != "yes" ] || echo "options : $getopt_temp"

wgetquiet='-q'
wgetfast='-t 3 -T 15'
wgetprogress='-v --progress=dot:default'
[ "$quiet" != "no" ] || wgetquiet=""
[ "$fast" != "no" ] || wgetfast=""
[ "$verbose" != "no" ] || wgetprogress=""
wgetoptions="$wgetquiet $wgetfast $wgetprogress"

arch=""
case $(dpkg --print-architecture) in
	amd64)	arch="x86_64" ;;
	i?86)	arch="i386" ;;
	*)
		die_hard "unsupported architectures" ;;
esac

cachedir=/var/cache/pepperflashplugin-nonfree

upstream=""
installed=""
if [ "$ACTION" = "--install" ] || [ "$ACTION" = "--status" ]
then
	if [ -f /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so ]
	then
		installed=$(strings /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so | awk '/LNX/{gsub(/,/,".");print$2}')
	fi
	[ "$verbose" != "yes" ] || echo "getting version of upstream"
	upstream=$(HOME=/root wget -q -O- "https://get.adobe.com/flashplayer/webservices/json/?platform_type=Linux&platform_arch=$arch&browser_dist=Chrome" | tr , '\n' | awk -F: '/"[Vv][Ee][Rr][Ss][Ii][Oo][Nn]":/{gsub(/ *",*/,"",$2);print$2;exit}')
	[ "$upstream" != "" ] || die_hard "failed to determine upstream version"
fi

case "$ACTION" in

	--install)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		if [ "$upstream" != "" ] && [ "$installed" != "" ] && [ "$upstream" = "$installed" ]
		then

			[ "$verbose" != "yes" ] || echo "upstream version $upstream is already installed"

		else
			cached=""
			if [ -f /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so ] && [ -f /var/cache/pepperflashplugin-nonfree/manifest.json ]
			then
				cached=$(strings /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so | awk '/LNX/{gsub(/,/,".");print$2}')
			fi
			if [ "$cached" != "" ] && [ "$cached" = "$upstream" ]
			then
				[ "$verbose" != "yes" ] || echo "using /var/cache/pepperflashplugin-nonfree copies"
				install -m 644 /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so /usr/lib/pepperflashplugin-nonfree/
				install -m 644 /var/cache/pepperflashplugin-nonfree/manifest.json /usr/lib/pepperflashplugin-nonfree/
			else
				url="https://fpdownload.adobe.com/pub/flashplayer/pdc/$upstream/flash_player_ppapi_linux.$arch.tar.gz"
				[ "$verbose" != "yes" ] || echo "downloading from $url"
				mkdir -p /var/cache/pepperflashplugin-nonfree
				HOME=/root wget $wgetoptions -O- "$url" | tar -xz -C /var/cache/pepperflashplugin-nonfree libpepflashplayer.so manifest.json
				chown root:root /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so
				chmod 644 /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so
				chown root:root /var/cache/pepperflashplugin-nonfree/manifest.json
				chmod 644 /var/cache/pepperflashplugin-nonfree/manifest.json
				cached=$(strings /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so | awk '/LNX/{gsub(/,/,".");print$2}')
				[ "$cached" = "$upstream" ] || die_hard "failed to download expected version $upstream, downloaded $cached"
				install -m 644 /var/cache/pepperflashplugin-nonfree/libpepflashplayer.so /usr/lib/pepperflashplugin-nonfree/
				install -m 644 /var/cache/pepperflashplugin-nonfree/manifest.json /usr/lib/pepperflashplugin-nonfree/
			fi
		fi

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"
		;;

	--uninstall)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		[ "$verbose" != "yes" ] || echo "removing files ..."
		rm -f /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so
		rm -f /usr/lib/pepperflashplugin-nonfree/manifest.json

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	--status)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		echo "Flash Player version installed on this system  : $installed"
		echo "Flash Player version available on upstream site: $upstream"

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	*)

		show_usage

		;;

esac

[ "$verbose" != "yes" ] || echo "end of update-pepperflashplugin-nonfree"

exit 0
